<template>
  <div class="page-container">
    <div class="tool-container">
      <el-form class="tool-search" :model="queryParams" ref="queryFormRef" :inline="true">
        <el-form-item label="" prop="groupCode">
          <el-input v-model="queryParams.groupCode" placeholder="请输入用户组编码" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="" prop="groupName">
          <el-input v-model="queryParams.groupName" placeholder="请输入用户组名称" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery" v-hasPermi="['system:group:list']">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="tool-handle">
        <el-button type="primary" icon="Plus" @click="handleAdd" v-hasPermi="['system:group:add']">新增分组</el-button>
      </div>
    </div>
    <div class="tabel-container">
      <vxe-grid ref="tableRef" v-bind="gridOptions" @sortChange="handleSortChange">
        <template #status="{ row }">
          <dict-tag :options="sys_group_status" :value="row.status" />
        </template>
        <template #handle="{ row }">
          <el-tooltip content="修改" placement="top">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(row)" v-hasPermi="['system:group:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" icon="Delete" @click="handleDelete(row)" v-hasPermi="['system:group:remove']"></el-button>
          </el-tooltip>
        </template>
        <template #bottom>
          <pagination
            v-show="total > 0"
            :total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
          />
        </template>
      </vxe-grid>
    </div>
    <module-edit ref="refModuleEdit" @success="getList"></module-edit>
  </div>
</template>

<script setup name="Group" lang="ts">
import { listGroup, delGroup } from '@/api/system/group';
import { GroupForm, GroupQuery, GroupVO } from '@/api/system/group/types';
import { getTableConfig } from './columns';
import ModuleEdit from './module/edit/index.vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_group_status } = toRefs<any>(proxy?.useDict('sys_group_status'));
const gridOptions = reactive(getTableConfig());
const data = toRef(gridOptions, 'data');
const loading = toRef(gridOptions, 'loading');
const refModuleEdit = ref();

const total = ref(0);

const queryFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: GroupForm = {
  id: undefined,
  groupCode: undefined,
  groupName: '',
  groupSort: 0,
  remark: '',
  status: '0'
};
const pageData = reactive<PageData<GroupForm, GroupQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 20,
    groupCode: '',
    groupName: '',
    status: ''
  },
  rules: {}
});

const { queryParams, form } = toRefs(pageData);

/** 查询租户列表 */
const getList = async () => {
  loading.value = true;
  const res = await listGroup(queryParams.value);
  data.value = res.rows;
  total.value = res.total;
  loading.value = false;
};
const handleSortChange = (e: any) => {
  console.log(e);
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 搜索重置操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};
/** 新增按钮操作 */
const handleAdd = () => {
  refModuleEdit?.value.open();
};

/** 修改按钮操作 */
const handleUpdate = async (row?: GroupVO) => {
  refModuleEdit?.value.openById(row?.id);
};

/** 删除按钮操作 */
const handleDelete = async (row?: GroupVO) => {
  const _ids = row?.id || '';
  await proxy?.$modal.confirm('是否确认删除用户组编号为"' + row?.groupCode + '"的数据项？');
  loading.value = true;
  await delGroup(_ids).finally(() => (loading.value = false));
  await getList();
  proxy?.$modal.msgSuccess('删除成功');
};

onMounted(() => {
  getList();
});
</script>
